package com.carlinfo.users.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.carlinfo.common.util.PageInfoUtil;
import com.carlinfo.users.dao.IAdminDao;
import com.carlinfo.users.pojo.AAdmin;
import com.carlinfo.users.service.IAdminService;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;

/**
 * @author Gonsen
 * 管理员service的实现类
 */
@Service("adminService")
public class AdminServiceImpl implements IAdminService {
	//@Resource
	@Autowired
	private IAdminDao adminDao;
	
	/**
	 * 保存一条数据
	 * */
	@Override
	public JSONObject saveOneService(AAdmin admin) {
		JSONObject resultJSON = new JSONObject();
		int res = this.adminDao.saveOne(admin);
		
		if(res > 0){
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("dffect", res + "");
			dataJSON.put("id", admin.getId() + "");
			/*把所需数据存放到resultJSON中*/
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else{
			
			resultJSON.put("code", "-1");
			resultJSON.put("info", "失败"	);
		}
		return resultJSON;
	}

	/**
	 * 查询一条数据
	 * */
	@Override
	public AAdmin findOneAdminSercice(Map<String, Object> conMap) {
		return this.adminDao.findOne(conMap);
	}

	/**
	 * 查询多条数据信息
	 * 分页
	 * */
	@Override
	public List<AAdmin> findCondAdminList(PageInfoUtil pageInfoUtil,Map<String, Object> condMap) {
		if(pageInfoUtil != null){
			Page<AAdmin> page = PageMethod.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());//当前页  每页多少条
			List<AAdmin> adminList = this.adminDao.findCondList(condMap);
			/* 设置总记录数 */
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
			return adminList;
		}
		return this.adminDao.findCondList(condMap);
	}

	/**
	 * 更新一条数据
	 * */
	@Override
	public JSONObject updateOneService(AAdmin admin) {
		JSONObject resultJSON = new JSONObject();
		int res = this.adminDao.updateOne(admin);
		
		if(res > 0){
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("dffect", res + "");
			dataJSON.put("id", admin.getId() + "");
			/*把所需数据存放到resultJSON中*/
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else{
			
			resultJSON.put("code", "-1");
			resultJSON.put("info", "失败"	);
		}
		return resultJSON;
	}

	/**
	 * 删除一条数据
	 * */
	@Override
	public JSONObject deleteOneService(Map<String, Object> condMap) {
		JSONObject resultJSON = new JSONObject();
		int res = this.adminDao.deleteOne(condMap);
		
		if(res > 0){
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("dffect", res + "");
			dataJSON.put("id", condMap.get("id") + "");
			/*把所需数据存放到resultJSON中*/
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else{
			
			resultJSON.put("code", "-1");
			resultJSON.put("info", "失败"	);
		}
		return resultJSON;
	}

}
